//
//   Copyright 2011 Gynvael Coldwind & Mateusz "j00ru" Jurczyk
//
//   Licensed under the Apache License, Version 2.0 (the "License");
//   you may not use this file except in compliance with the License.
//   You may obtain a copy of the License at
//
//       http://www.apache.org/licenses/LICENSE-2.0
//
//   Unless required by applicable law or agreed to in writing, software
//   distributed under the License is distributed on an "AS IS" BASIS,
//   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
//   See the License for the specific language governing permissions and
//   limitations under the License.
//
#include <cstdio>
#include <cstdlib>
#include <string>
#include <SDL/SDL_mixer.h>
#include "music.h"
#include "global.h"
using namespace std;

map<string, Mix_Music*> g_MusicContainer;
map<string, Mix_Chunk*> g_SoundContainer;

bool music_Init()
{
  /* We're going to be requesting certain things from our audio
     device, so we set them up beforehand */
  int audio_rate = 22050;
  Uint16 audio_format = AUDIO_S16; /* 16-bit stereo */
  int audio_channels = 2;
  int audio_buffers = 4096;

  /* This is where we open up our audio device.  Mix_OpenAudio takes
     as its parameters the audio format we'd /like/ to have. */
  if(Mix_OpenAudio(audio_rate, audio_format, audio_channels, audio_buffers)) {
    printf("Unable to open audio!\n");
    exit(1);
  }

  // Already insert some sounds at this point
  music_AddSound("sfx/beep-3.wav",            SND_DENY);
  music_AddSound("sfx/saber.wav",             SND_SHOOT);
  music_AddSound("sfx/smack-1.wav",           SND_HIT);
  music_AddSound("sfx/shower-door-2.wav",     SND_DOOR);
  music_AddSound("sfx/button-30.wav",         SND_MENU_CHANGE);
  music_AddSound("sfx/button-21.wav",         SND_MENU_SELECT);
  music_AddSound("sfx/applause-1.wav",        SND_SUCCESS);
  music_AddSound("sfx/button-8.wav",          SND_TELEPORT);
  music_AddSound("sfx/fail-trombone-03.wav",  SND_FAILURE);

  // Set sound volume
  Mix_Volume(-1, 20);
  Mix_VolumeMusic(25);

  return true;
}

bool music_AddSound(char* filename, char* name)
{
  g_SoundContainer[string(name)] = Mix_LoadWAV(filename);
  if(g_SoundContainer[string(name)] == NULL)
  {
    printf("error: unable to load sound \"%s\" from file \"%s\", error %s\n", name, filename, Mix_GetError());
    return false;
  }

  return true;
}

bool music_AddMusic(char* filename, char* name)
{
  g_MusicContainer[string(name)] = Mix_LoadMUS(filename);
  if(g_MusicContainer[string(name)] == NULL)
  {
    printf("error: unable to load music \"%s\" from file \"%s\", error %s\n", name, filename, Mix_GetError());
    return false;
  }
  
  return true;
}

void music_playSound(char* name)
{
  Mix_PlayChannel(-1, g_SoundContainer[string(name)], 0);
}

void music_playMusic(char* name)
{
  Mix_PlayMusic(g_MusicContainer[string(name)], -1);
}

void music_Stop()
{
  Mix_HaltMusic();
}

bool music_SoundsExists(char* name)
{
  return (g_SoundContainer.find(string(name)) != g_SoundContainer.end());
}

bool music_MusicExists(char* name)
{
  return (g_MusicContainer.find(string(name)) != g_MusicContainer.end());
}

